package com.springboot.controller.web;

import com.springboot.dto.DoctorViewPatient;
import com.springboot.dto.WebDoctorViewPatient;
import com.springboot.model.DoctorInfoForm;
import com.springboot.model.OrderForm;
import com.springboot.model.UserLoginForm;
import com.springboot.service.DoctorViewAppointDetailsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
@Controller
public class DoctorSeeOrderController {
    @Autowired
    DoctorViewAppointDetailsService doctorViewAppointDetailsService;
    /**
     * web端医生查看今日预约详情
     */
    @CrossOrigin
    @RequestMapping(value = "/webgetPatientTodayServlet")
    public @ResponseBody
    String webDoctorViewToday(HttpServletRequest request) {
        HttpSession session = request.getSession();
        String tel = (String)session.getAttribute("tel");
        System.out.println("tel:"+tel);
        System.out.println("WEB:医生查看今日预约详情");
        try {
            DoctorInfoForm doctorInfoForm=doctorViewAppointDetailsService.findDoctorByTel(tel);
            int id=doctorInfoForm.getDoctorId();
            //通过医生ID查找该医生的预约信息
            ArrayList<OrderForm> orderForms=doctorViewAppointDetailsService.findOrderBydoctorId(id);
            if(orderForms.size()==0)
            {
                return "该医生没有预约信息";
            }
            else
            {
                SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");//日期格式
                String current_date=format.format(System.currentTimeMillis());
                int dateNum=0;
                ArrayList<OrderForm> orderFormsToday=new ArrayList<>();
                //数组存储预约日期是今天，并且进度为未就诊的患者id
                int []patientId=new int[orderForms.size()+1];
                //预约日期是今天，并且进度为未就诊的预约id
                int []bookId=new int[orderForms.size()+1];
                String []bookDate=new String[orderForms.size()+1];
                String []bookTime=new String[orderForms.size()+1];
                for(int i=0;i<orderForms.size();i++)
                {
                    OrderForm orderForm=orderForms.get(i);
                    String book_date=format.format(orderForm.getBootDate());
                    if(book_date.equals(current_date)&&orderForm.getProgress().equals("未就诊"))
                    {
                        orderFormsToday.add(orderForm);
                        patientId[dateNum]=orderForm.getPatientId();
                        bookId[dateNum]=orderForm.getBootId();
                        bookDate[dateNum]=format.format(orderForm.getBootDate());
                        bookTime[dateNum]=orderForm.getBootTime();
                        dateNum++;
                        System.out.println(orderForm.getBootId());
                    }
                }
                //doctorViewPatients存储返回给前端的model，即该医生的今日未就诊患者
                ArrayList<WebDoctorViewPatient> doctorViewPatients=new ArrayList<>();
                for(int i=0;i<dateNum;i++)
                {
                    WebDoctorViewPatient doctorViewPatient=new WebDoctorViewPatient();
                    doctorViewPatient.setOrderDate(bookDate[i]);
                    doctorViewPatient.setOrderTime(bookTime[i]);
                    UserLoginForm userLoginForm=doctorViewAppointDetailsService.findUserLogo(patientId[i]);
                    doctorViewPatient.setLogoLink(userLoginForm.getLogoLink());
                    doctorViewPatient.setPatientName(userLoginForm.getUserName());
                    doctorViewPatient.setPatientTel(userLoginForm.getUserTel());
                    doctorViewPatient.setBookId(bookId[i]);
                    doctorViewPatients.add(doctorViewPatient);
                    System.out.println(doctorViewPatient.toString());
                }

                //存入的是WebDoctorViewPatient
                session.setAttribute("doctorViewPatients",doctorViewPatients);

                return doctorViewPatients.toString();
            }
        } catch (Exception e) {
            return "页面错误";
        }
    }

    /**
     * web端医生查看近期预约详情
     */
    @CrossOrigin
    @RequestMapping(value = "/webgetPatientServlet")
    public @ResponseBody
    String webDoctorViewRecent(HttpServletRequest request) {
        System.out.println("WEB:医生查看近期预约详情");
        try
        {
            HttpSession session = request.getSession();
            String tel = (String)session.getAttribute("tel");
                DoctorInfoForm doctorInfoForm=doctorViewAppointDetailsService.findDoctorByTel(tel);
                int id=doctorInfoForm.getDoctorId();
                //通过医生ID查找该医生的预约信息
                ArrayList<OrderForm> orderForms=doctorViewAppointDetailsService.findOrderBydoctorId(id);
                if(orderForms.size()==0)
                {
                    ArrayList<DoctorViewPatient> doctorViewPatients = new ArrayList<>();
                    session.setAttribute("doctorViewPatients2",doctorViewPatients);
                    return "该医生没有预约信息";
                }
                else
                {
                    SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");//日期格式
                    String current_date=format.format(System.currentTimeMillis());
                    int dateNum=0;
                    //数组存储预约日期大于今天，并且进度为未就诊的患者id
                    int []patientId=new int[orderForms.size()+1];
                    //预约日期大于今天，并且进度为未就诊的预约id
                    int []bookId=new int[orderForms.size()+1];
                    //预约日期
                    String []bookDate=new String[orderForms.size()+1];
                    String []bookTime=new String[orderForms.size()+1];
                    for(int i=0;i<orderForms.size();i++)
                    {
                        OrderForm orderForm=orderForms.get(i);
                        String book_date=format.format(orderForm.getBootDate());
                        if(book_date.compareTo(current_date)>0&&orderForm.getProgress().equals("未就诊"))
                        {
                            patientId[dateNum]=orderForm.getPatientId();
                            bookId[dateNum]=orderForm.getBootId();
                            bookDate[dateNum]=format.format(orderForm.getBootDate());
                            bookTime[dateNum]=orderForm.getBootTime();
                            dateNum++;
                            System.out.println(orderForm.getBootId());
                        }
                    }
                    //doctorViewPatients存储返回给前端的model，即该医生的今日未就诊患者
                    ArrayList<WebDoctorViewPatient> doctorViewPatients=new ArrayList<>();
                    for(int i=0;i<dateNum;i++)
                    {
                        WebDoctorViewPatient doctorViewPatient=new WebDoctorViewPatient();
                        doctorViewPatient.setOrderDate(bookDate[i]);
                        doctorViewPatient.setOrderTime(bookTime[i]);
                        UserLoginForm userLoginForm=doctorViewAppointDetailsService.findUserLogo(patientId[i]);
                        doctorViewPatient.setLogoLink(userLoginForm.getLogoLink());
                        doctorViewPatient.setPatientName(userLoginForm.getUserName());
                        doctorViewPatient.setPatientTel(userLoginForm.getUserTel());
                        doctorViewPatient.setBookId(bookId[i]);
                        doctorViewPatients.add(doctorViewPatient);
                        System.out.println(doctorViewPatient.toString());
                    }
                    if(dateNum>=0)
                    {
                        session.setAttribute("doctorViewPatients2",doctorViewPatients);
                        return doctorViewPatients.toString();
                    }
                    else
                    {
                        return "空";
                    }
                }
        } catch (Exception e) {
            return "页面错误";
        }
    }
}
